package com.gitee.hljdrl.ffkit.app;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.gitee.hljdrl.ffkit.R;
import com.gitee.hljdrl.ffkit.basis.FFKitEvent;
import com.gitee.hljdrl.ffkit.bean.FFKitLink;
import com.gitee.hljdrl.ffkit.databinding.FfkitActivtySupporterBinding;
import com.gitee.hljdrl.ffkit.util.FFKitUtil;
import com.gitee.hljdrl.utils.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.List;

public class FFSupporterActivity extends FFKitActivity {

    public static void startLink(Activity activity, FFKitLink link) {
        Intent intent = new Intent(activity, FFSupporterActivity.class);
        //
        Bundle bundle = new Bundle();
        bundle.putString(FFKitEvent.KEY_TITLE, link.name);
        //---------------------------------------------
        if (link.fragmentClass != null) {
            intent.putExtra(FFKitEvent.KEY_CLASS, link.fragmentClass.getCanonicalName());
        }
        if (link.fragmentClassList != null) {
            ArrayList<String> list = new ArrayList<>();
            List<Class<? extends Fragment>> classList = link.fragmentClassList;
            for (Class cls : classList) {
                list.add(cls.getCanonicalName());
            }
            intent.putStringArrayListExtra(FFKitEvent.KEY_CLASS_LIST, list);
        }
        //---------------------------------------------
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    private FfkitActivtySupporterBinding mBinding;

    @Override
    public View getBindingView() {
        mBinding = FfkitActivtySupporterBinding.inflate(getLayoutInflater());
        return mBinding.getRoot();
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        //
        Toolbar toolbar = findViewById(R.id.tool_bar);
        toolbar.setTitle(intent.getStringExtra(FFKitEvent.KEY_TITLE));
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);//这2行是设置返回按钮的
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
        toolbar.getNavigationIcon().setTint(Color.WHITE);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (intent.hasExtra(FFKitEvent.KEY_CLASS)) {
            try {
                Class<Fragment> clazz = (Class<Fragment>) Class.forName(intent.getStringExtra(FFKitEvent.KEY_CLASS));
                injectFragment(clazz.newInstance(), fragmentTransaction);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        if (intent.hasExtra(FFKitEvent.KEY_CLASS_LIST)) {
            ArrayList<String> classList = intent.getStringArrayListExtra(FFKitEvent.KEY_CLASS_LIST);
            if (CollectionUtil.isEnable(classList)) {
                for (String cls : classList) {
                    try {
                        Class<Fragment> clazz = (Class<Fragment>) Class.forName(cls);
                        injectFragment(clazz.newInstance(), fragmentTransaction);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        //
//        fragmentTransaction.commitAllowingStateLoss();
        fragmentTransaction.commit();
    }

    void injectFragment(Fragment targetFragment, FragmentTransaction fragmentTransaction) {
        //----------------------------------------------------
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            extras = new Bundle();
        }
        targetFragment.setArguments(extras);
        //----------------------------------------------------
        LinearLayout linearLayout = new LinearLayout(this);
        final int viewId = View.generateViewId();
        linearLayout.setId(viewId);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        linearLayout.setBackground(FFKitUtil.getRandomColorDrawable());
        mBinding.mainContent.addView(linearLayout);
        //----------------------------------------------------
        fragmentTransaction.add(viewId, targetFragment).show(targetFragment);

    }


    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

    }
}
